﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace App
{
    public partial class DT : UserControl
    {
        DateTime m_Dt = DateTime.MinValue;
        private PictureBox pictureBox1;
        private Txt textBox1;
        private static int DropDownButtonWidth = 17;
        public DT()
        {
            InitializeComponent();
        }

        private System.ComponentModel.IContainer components = null;
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region InitializeComponent
        private void InitializeComponent()
        {
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.textBox1 = new App.Txt();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.pictureBox1.BackColor = System.Drawing.Color.Transparent;
            this.pictureBox1.Location = new System.Drawing.Point(43, 2);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(16, 16);
            this.pictureBox1.TabIndex = 1;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.Click += new System.EventHandler(this.button1_Click);
            this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
            // 
            // textBox1
            // 
            this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox1.BackColor = System.Drawing.Color.White;
            this.textBox1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(141)))), ((int)(((byte)(178)))), ((int)(((byte)(227)))));
            this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.textBox1.Font = new System.Drawing.Font("Verdana", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.textBox1.HotColor = System.Drawing.Color.FromArgb(((int)(((byte)(147)))), ((int)(((byte)(157)))), ((int)(((byte)(9)))));
            this.textBox1.Location = new System.Drawing.Point(0, 0);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(60, 21);
            this.textBox1.TabIndex = 0;
            this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            // 
            // DT
            // 
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.textBox1);
            this.Name = "DT";
            this.Size = new System.Drawing.Size(60, 21);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion

        public DateTime Value
        {
            get
            {
                return m_Dt;
            }
            set
            {
                m_Dt = value;
                if (m_Dt != DateTime.MinValue)
                    textBox1.Text = m_Dt.ToShortDateString();
            }
        }

        public string ValueText
        {
            get
            {
                return textBox1.Text;
            }
            set
            {
                textBox1.Text = value;
            }
        }

        public bool IsNull
        {
            get
            {
                return string.IsNullOrEmpty(textBox1.Text);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form frm = new Form();
            frm.FormBorderStyle = FormBorderStyle.None;
            frm.StartPosition = FormStartPosition.Manual;
            frm.BackColor = Color.White;
            frm.Deactivate += delegate(object sf2, EventArgs ef2)
            {
                frm.Close();
            };
            MonthCalendar dp = new MonthCalendar();
            frm.Width = dp.Width + 65;
            frm.Height = dp.Height + 12;
            dp.Dock = DockStyle.Fill;
            if (!string.IsNullOrEmpty(textBox1.Text))
            {
                dp.SetDate(m_Dt);
            }
            dp.DateSelected += delegate(object s2, DateRangeEventArgs e2)
            {
                Value = e2.End;
                frm.Close();
            };
            frm.Controls.Add(dp);
            dp.Dock = DockStyle.Fill;

            Point p = this.PointToScreen(pictureBox1.Location);

            frm.Location = p;
            frm.Show();

        }



        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            DateTime result = DateTime.MinValue;
            if (DateTime.TryParse(textBox1.Text, out result))
            {
                if (Value != DateTime.Parse(textBox1.Text))
                {
                    Value = DateTime.Parse(textBox1.Text);
                }
                textBox1.BackColor = Color.White;
            }
            else
            {
                if (!IsNull)
                {
                    textBox1.BackColor = Color.Red;
                }
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            PaintFlatDropDown(pictureBox1, e.Graphics);
        }

        public static void PaintFlatDropDown(Control ctrl, Graphics g)
        {
            Rectangle rect = new Rectangle(ctrl.Width - DropDownButtonWidth, 0, DropDownButtonWidth, ctrl.Height);
            //ControlPaint.DrawComboButton(g, rect, ButtonState.Flat);
            g.FillRectangle(Brushes.White, rect);
            int len = 6;
            int x = ctrl.Width - 6;
            int y = ctrl.Height / 2 - 2;
            if (x <= 6 || y <= 1) return;
            Point[] pntArr = new Point[3];
            pntArr[0] = new Point(x, y);
            pntArr[1] = new Point(x - len, y);
            pntArr[2] = new Point(x - len / 2, (int)(len * Math.Sqrt(3) / 2 + y - 2));
            System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
            path.AddLines(pntArr);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            g.FillPath(Brushes.Black, path);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
            path.Dispose();
        }
    }
}
